package leetcode.lt100;

class Solution_73 {

	public static void main(String[] args) {
		int[][] matrix = { { 1, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 } };

		int m = matrix.length;
		int n = matrix[0].length;
		for (int i = 0; i < m; i++) {
			StringBuffer sb = new StringBuffer();
			for (int j = 0; j < n; j++) {
				sb.append(matrix[i][j] + " ");
			}
			System.out.println(sb);
		}
		setZeroes(matrix);
		System.out.println("");
		for (int i = 0; i < m; i++) {
			StringBuffer sb = new StringBuffer();
			for (int j = 0; j < n; j++) {
				sb.append(matrix[i][j] + " ");
			}
			System.out.println(sb);
		}
	}

	public static void setZeroes(int[][] matrix) {
		int m = matrix.length;
		int n = matrix[0].length;

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (matrix[i][j] == 0)
					resetByInt(matrix, m, n, i, j, 587456789);
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (matrix[i][j] == 587456789)
					matrix[i][j] = 0;
			}
		}

	}

	private static void resetByInt(int[][] matrix, int m, int n, int x, int y, int num) {

		for (int i = 0; i < n; i++) {
			if (matrix[x][i] != 0)
				matrix[x][i] = 587456789;
		}

		for (int i = 0; i < m; i++) {
			if (matrix[i][y] != 0)
				matrix[i][y] = 587456789;
		}

	}

}